package com.liusu.spider.encrypt;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.junit.jupiter.api.Test;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.nio.channels.Pipe;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class Fate {



    @Test
    public void context(){

        /*int a = 'a';
        System.out.println(a);*/


        /*for (int i = 0; i < 128; i++) {
            System.out.println((char) i);
        }*/

        /**
         * 127
         * 130-128
         */
        /*String m = "only my railgun forever";
        String m1 = "rqo|#p|#udlojxq#iruhyhu";
        System.out.println(decrypt(m1,3));*/







        String a = "苏";

        byte[] bytes = a.getBytes();
        for (byte b : bytes) {
            System.out.println(b);
            System.out.println(Integer.toBinaryString(b));
        }


    }

    /**
     * 加密方法
     * @param content
     * @param key
     * @return
     */
    public String encrypt(String content,int key){

        char[] chars = content.toCharArray();
        StringBuilder builder = new StringBuilder();
        for (char c : chars) {
            int t = (int) c;
            t = t + key;
            if(t>127){
                t = t-128;
            }
            builder.append((char) t);
        }
        return builder.toString();
    }
    /**
     * 解密方法
     * @param content
     * @param key
     * @return
     */
    public String decrypt(String content,int key){

        char[] chars = content.toCharArray();
        StringBuilder builder = new StringBuilder();
        for (char c : chars) {
            int t = (int) c;
            t = t - key;
            if(t<0){
                t = t+128;
            }
            builder.append((char) t);
        }
        return builder.toString();
    }


    @Test
    public void decrypt(){

        String content = "唯我超电磁炮永世长存!";
        String key = "12345678";
        /**
         * 加密算法
         */
        String transforms = "DES";
        /**
         *
         */
        try {
            Cipher cipher = Cipher.getInstance(transforms);
            /**
             * 加密对象
             * [1]密钥的字节
             * [2]加密类型
             */
            SecretKeySpec spec = new SecretKeySpec(key.getBytes(),transforms);
            /**
             * [1]加密/解密
             * [2]加密规则
             */
            cipher.init(Cipher.ENCRYPT_MODE,spec);
            byte[] bytes = cipher.doFinal(content.getBytes());
            /*for (byte b : bytes) {
                System.out.println(b);
            }*/
            String mw = Base64.encode(bytes);
            System.out.println(mw);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
    }
    /*
    * */
    @Test
    public void context1(){
        String content = "7UZghuz9DSuSqMChKkJ9CKUlO7Akd7wdCfcxU/QYQyQ=";
        String key = "12345678";
        String dm = DES(content,key);

        System.out.println(dm);
    }
    /**
     *
     * @param content
     * @param k
     */
    public String DES(String content,String k){

        /**
         * 加密算法
         */
        String transforms = "DES";
        /**
         *
         */
        try {
            Cipher cipher = Cipher.getInstance(transforms);
            /**
             * 加密对象
             * [1]密钥的字节
             * [2]加密类型
             */
            SecretKeySpec spec = new SecretKeySpec(k.getBytes(),transforms);
            /**
             * [1]加密/解密
             * [2]加密规则
             */
            cipher.init(Cipher.DECRYPT_MODE,spec);

            /**
             * A-Z
             * a-z
             * + /
             */
            byte[] bytes = cipher.doFinal(Base64.decode(content));
            /*for (byte b : bytes) {
                System.out.println(b);
            }*/
            return new String(bytes);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * base58
     * 1-9          9
     * a-h j-n p-z  24
     * A-H J-Z      25
     */
}
